package com.doodod.common.utils;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: 邱孝兵
 * Date: 14-4-24
 * Time: 上午9:55
 */
public class YouPaiUtil {
    static UpYun upyun;

    static {
        upyun = new UpYun("doodod-gamecenter", "doodod", "doodod123");
        upyun.setDebug(true);
        upyun.setTimeout(600);
        upyun.setApiDomain(UpYun.ED_AUTO);
    }

    /**
     * 上传文件到又拍服务器
     *
     * @param fullPath
     * @param srcFile
     * @return
     * @throws IOException
     */
    public static boolean uploadFile(String fullPath, File srcFile) throws IOException {
        return upyun.writeFile(fullPath, srcFile, true);
    }

    /**
     * 从又拍服务器下载文件
     *
     * @param fullPath
     * @param dstFile
     * @return
     */
    public static boolean downloadFile(String fullPath, File dstFile) {
        return upyun.readFile(fullPath, dstFile);
    }

    /**
     * 将文件从又拍服务器删除
     *
     * @param fullPath
     * @return
     */
    public static boolean delFile(String fullPath) {
        return upyun.deleteFile(fullPath);
    }

    public static String getDirPrefix() {
        String prefix = "";
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
        String time = sdf.format(date);

        String year = time.substring(0, 4);
        String month = time.substring(4, 6);
        String day = time.substring(6, 8);
        String fileprefix = time.substring(8);

        return prefix + year + "/" + month + "/" + day + "/" + fileprefix;
    }

    public static void main(String [] args){
        System.out.println(getDirPrefix());
    }
}
